<template>
  <div class="applicant-list">
    <!-- 搜索功能区 -->
    <div class="function-area">
      <div class="search-section">
        <div class="search-item">
          <label>请输入监考人姓名/工号关键词查询：</label>
          <input
              type="text"
              v-model="searchKeyword"
              placeholder="输入姓名或工号"
              class="search-input"
          >
        </div>

        <div class="action-buttons">
          <button class="query-btn" @click="searchApplicants">查询</button>
          <button class="export-btn" @click="exportData">数据导出</button>
        </div>
      </div>
    </div>

    <!-- 报名人员表格 -->
    <div class="applicant-table">
      <table>
        <thead>
        <tr>
          <th>序号</th>
          <th>姓名</th>
          <th>性别</th>
          <th>工号</th>
          <th>所在单位</th>
          <th>审批结束时间</th>
          <th>审批状态</th>
          <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="(applicant, index) in filteredApplicants" :key="applicant.id">
          <td>{{ index + 1 }}</td>
          <td>
            <a href="javascript:void(0)" @click="previewApplication(applicant)">
              {{ applicant.name }}
            </a>
          </td>
          <td>{{ applicant.gender }}</td>
          <td>{{ applicant.workId }}</td>
          <td>{{ applicant.department }}</td>
          <td>{{ formatDate(applicant.approvalTime) }}</td>
          <td>
              <span :class="getStatusClass(applicant.status)">
                {{ applicant.statusText }}
              </span>
          </td>
          <td>
            <button class="view-btn" @click="previewApplication(applicant)">查看审批</button>
          </td>
        </tr>
        </tbody>
      </table>

      <div class="pagination-info">
        总共 {{ filteredApplicants.length }} 个
      </div>
    </div>

    <!-- 报名表预览弹窗 - 完整复制自 ExamManagement.vue -->
    <div v-if="showPreview" class="preview-modal">
      <div class="modal-content">
        <div class="modal-header">
          <h3>四川大学锦江学院研究生招生考试监考人员报名表</h3>
          <button class="close-btn" @click="showPreview = false">&times;</button>
        </div>

        <div class="modal-body">
          <div class="form-row">
            <div class="form-col">
              <div class="form-item">
                <label>姓名：</label>
                <span>{{ previewData.name }}</span>
              </div>
              <div class="form-item">
                <label>性别：</label>
                <span>{{ previewData.gender }}</span>
              </div>
              <div class="form-item">
                <label>学历：</label>
                <span>{{ previewData.education }}</span>
              </div>
              <div class="form-item">
                <label>出生年月：</label>
                <span>{{ previewData.birthDate }}</span>
              </div>
            </div>

            <div class="form-col">
              <div class="form-item">
                <label>单位：</label>
                <span>{{ previewData.department }}</span>
              </div>
              <div class="form-item">
                <label>工号：</label>
                <span>{{ previewData.workId }}</span>
              </div>
              <div class="form-item">
                <label>手机号：</label>
                <span>{{ previewData.phone }}</span>
              </div>
              <div class="form-item">
                <label>备用电话：</label>
                <span>{{ previewData.backupPhone }}</span>
              </div>
            </div>
          </div>

          <div class="form-section">
            <label>电子照片：</label>
            <div class="photo-placeholder">
              <div class="plus-icon">+</div>
              <div class="upload-text">上传照片</div>
            </div>
          </div>

          <div class="form-section">
            <label>监考校区：</label>
            <div class="campus-options">
              <div v-for="(campus, index) in campuses" :key="index" class="campus-option">
                <input
                    type="checkbox"
                    :id="'campus'+index"
                    :checked="campus.checked"
                    disabled
                >
                <label :for="'campus'+index">{{ campus.name }}</label>
              </div>
            </div>
          </div>

          <div class="form-section conditions">
            <h4>监考人员基本条件：</h4>
            <ol>
              <li>政治立场坚定，思想品德良好，工作认真负责，能按时参加考务培训及考核且胜任监考工作；</li>
              <li>学校在编在岗教职工，身体健康，考前14天（12月10日-23日）健康监测情况正常；</li>
              <li>无直系亲属或者利害关系人参加本年度全国硕士研究生招生考试；</li>
              <li>熟悉考试政策规定、有监考工作经验者优先。</li>
            </ol>
          </div>

          <div class="form-section">
            <h4>申请人承诺：</h4>
            <p>本人自愿参加研究生入学考试监考工作，认真学习掌握工作纪律要求，严格遵守《国家教育考试考务安全保密工作规定》，切实遵守监考工作守则，按照考务工作安排，完成监考工作。</p>
            <div class="signature">
              <div>申请人：{{ previewData.name }}</div>
              <div>日期：{{ previewData.applyDate }}</div>
            </div>
          </div>

          <div class="form-section">
            <h4>所在单位意见：</h4>
            <div class="signature-area">
              <div class="signature-line">
                <span>领导签字：</span>
                <div class="signature-placeholder"></div>
              </div>
              <div class="signature-line">
                <span>单位盖章：</span>
                <div class="stamp-placeholder"></div>
              </div>
              <div class="signature-line">
                <span>日期：</span>
                <span>{{ previewData.applyDate }}</span>
              </div>
            </div>
          </div>

          <div class="form-section">
            <label>备注：</label>
            <div class="remarks">{{ previewData.remarks }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue';
import { useRoute } from 'vue-router';

const route = useRoute();
const batchId = route.params.batchId;

// 搜索关键词
const searchKeyword = ref('');

// 校区选项
const campuses = ref([
  { id: 'jj', name: '锦江校区', checked: true },
  { id: 'other', name: '其他校区', checked: false },
  { id: 'fc', name: '服从调剂', checked: true }
]);

// 报名人员数据
const applicants = ref([
  {
    id: 1,
    name: 'QIANJIAXIN1',
    gender: '男',
    workId: '10001',
    department: '计算机智能学操',
    approvalTime: '2023-10-17T16:57:35',
    status: 'approved',
    statusText: '同意报名',
    education: '博士',
    phone: '13800138000',
    backupPhone: '13800138001',
    birthDate: '1985-01-01',
    applyDate: '2023-09-15',
    remarks: '无特殊要求'
  },
  {
    id: 2,
    name: 'QIANJIAXIN2',
    gender: '男',
    workId: '10002',
    department: '计算机智能学操',
    approvalTime: '2023-10-17T16:57:35',
    status: 'approved',
    statusText: '同意报名',
    education: '硕士',
    phone: '13800138002',
    backupPhone: '13800138003',
    birthDate: '1988-05-12',
    applyDate: '2023-09-16',
    remarks: '南湖校区优先'
  },
  {
    id: 3,
    name: 'QIANJIAXIN3',
    gender: '男',
    workId: '10003',
    department: '计算机智能学操',
    approvalTime: '2023-10-17T16:57:35',
    status: 'approved',
    statusText: '同意报名',
    education: '博士',
    phone: '13800138004',
    backupPhone: '13800138005',
    birthDate: '1983-11-23',
    applyDate: '2023-09-17',
    remarks: '服从安排'
  },
  {
    id: 4,
    name: 'QIANJIAXIN4',
    gender: '男',
    workId: '10004',
    department: '计算机智能学操',
    approvalTime: '2023-10-17T16:57:35',
    status: 'approved',
    statusText: '同意报名',
    education: '硕士',
    phone: '13800138006',
    backupPhone: '13800138007',
    birthDate: '1990-02-14',
    applyDate: '2023-09-18',
    remarks: '无'
  },
  {
    id: 5,
    name: 'QIANJIAXINS',
    gender: '男',
    workId: '10005',
    department: '计算机智能学操',
    approvalTime: '2023-10-17T16:57:35',
    status: 'rejected',
    statusText: '不同意报名',
    education: '博士',
    phone: '13800138008',
    backupPhone: '13800138009',
    birthDate: '1979-07-30',
    applyDate: '2023-09-19',
    remarks: '时间冲突'
  }
]);

// 预览数据
const previewData = ref({
  name: '',
  department: '',
  gender: '',
  workId: '',
  education: '',
  phone: '',
  backupPhone: '',
  birthDate: '',
  applyDate: '',
  remarks: ''
});

// 弹窗控制
const showPreview = ref(false);

// 过滤后的报名人员
const filteredApplicants = computed(() => {
  if (!searchKeyword.value) return applicants.value;

  const keyword = searchKeyword.value.toLowerCase();
  return applicants.value.filter(applicant =>
      applicant.name.toLowerCase().includes(keyword) ||
      applicant.workId.includes(keyword)
  );
});

// 日期格式化
const formatDate = (dateString: string) => {
  const date = new Date(dateString);
  return `${date.getFullYear()}/${(date.getMonth() + 1).toString().padStart(2, '0')}/${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`;
};

// 获取状态类名
const getStatusClass = (status: string) => {
  return {
    'approved': 'status-approved',
    'rejected': 'status-rejected'
  }[status] || '';
};

// 搜索报名人员
const searchApplicants = () => {
  console.log('搜索关键词:', searchKeyword.value);
};

// 导出数据
const exportData = () => {
  alert('导出数据');
};

// 预览报名表
const previewApplication = (applicant: any) => {
  previewData.value = {
    name: applicant.name,
    department: applicant.department,
    gender: applicant.gender,
    workId: applicant.workId,
    education: applicant.education,
    phone: applicant.phone,
    backupPhone: applicant.backupPhone,
    birthDate: applicant.birthDate,
    applyDate: applicant.applyDate,
    remarks: applicant.remarks
  };
  showPreview.value = true;
};

onMounted(() => {
  console.log(`加载批次 ${batchId} 的报名人员`);
});
</script>

<style scoped>
.applicant-list {
  background-color: #f8fafc;
  padding: 20px;
  border-radius: 8px;
}

.function-area {
  background-color: white;
  padding: 20px;
  border-radius: 6px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  margin-bottom: 25px;
}

.search-section {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  align-items: center;
}

.search-item {
  flex: 1;
  display: flex;
  align-items: center;
}

.search-item label {
  font-weight: 600;
  color: #1a2b4b;
  min-width: 250px;
}

.search-input {
  padding: 8px 12px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  flex: 1;
}

.action-buttons {
  display: flex;
  gap: 12px;
}

.action-buttons button {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.3s;
}

.query-btn {
  background-color: #1a3a6c;
  color: white;
}

.query-btn:hover {
  background-color: #0d2a56;
}

.export-btn {
  background-color: #f0f2f5;
  color: #1a2b4b;
  border: 1px solid #d9d9d9;
}

.export-btn:hover {
  background-color: #e2e8f0;
}

.applicant-table {
  background-color: white;
  border-radius: 6px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 20px;
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th, td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #e8e8e8;
}

th {
  background-color: #f0f2f5;
  color: #1a2b4b;
  font-weight: 600;
}

tbody tr:hover {
  background-color: #f8fafc;
}

.status-approved, .status-rejected {
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 13px;
  font-weight: 500;
}

.status-approved {
  background-color: #f6ffed;
  color: #52c41a;
}

.status-rejected {
  background-color: #fff2f0;
  color: #ff4d4f;
}

.view-btn {
  padding: 6px 12px;
  background-color: #e6f7ff;
  color: #1890ff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
}

.view-btn:hover {
  background-color: #bae7ff;
}

.pagination-info {
  margin-top: 20px;
  text-align: right;
  font-size: 14px;
  color: #666;
}

/* 弹窗样式 */
.preview-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background-color: white;
  width: 90%;
  max-width: 900px;
  max-height: 90vh;
  border-radius: 8px;
  overflow-y: auto;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  background-color: #1a3a6c;
  color: white;
  border-top-left-radius: 8px;
  border-top-right-radius: 8px;
}

.modal-header h3 {
  margin: 0;
  font-size: 20px;
}

.close-btn {
  background: none;
  border: none;
  color: white;
  font-size: 28px;
  cursor: pointer;
  padding: 0 10px;
}

.modal-body {
  padding: 25px;
}

.form-row {
  display: flex;
  gap: 30px;
  margin-bottom: 20px;
}

.form-col {
  flex: 1;
}

.form-item {
  display: flex;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px dashed #e8e8e8;
}

.form-item label {
  font-weight: 600;
  min-width: 100px;
  color: #1a2b4b;
}

.form-section {
  margin-bottom: 25px;
}

.form-section label {
  font-weight: 600;
  color: #1a2b4b;
  display: block;
  margin-bottom: 10px;
}

.photo-placeholder {
  width: 150px;
  height: 200px;
  border: 1px dashed #d9d9d9;
  border-radius: 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #fafafa;
}

.plus-icon {
  font-size: 40px;
  color: #8c8c8c;
  line-height: 1;
}

.upload-text {
  margin-top: 10px;
  color: #8c8c8c;
}

.campus-options {
  display: flex;
  gap: 25px;
  margin-top: 10px;
}

.campus-option {
  display: flex;
  align-items: center;
}

.campus-option input {
  margin-right: 8px;
}

.conditions ol {
  padding-left: 25px;
}

.conditions li {
  margin-bottom: 10px;
  line-height: 1.6;
}

.signature {
  margin-top: 20px;
  text-align: right;
  font-weight: 600;
}

.signature-area {
  margin-top: 20px;
}

.signature-line {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.signature-line span {
  min-width: 100px;
  font-weight: 600;
}

.signature-placeholder {
  flex: 1;
  height: 30px;
  border-bottom: 1px solid #333;
}

.stamp-placeholder {
  width: 120px;
  height: 120px;
  border: 1px dashed #d9d9d9;
  background-color: #fafafa;
  margin-top: 10px;
}

.remarks {
  padding: 10px;
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  min-height: 80px;
  background-color: #fafafa;
}
</style>